﻿//created by Mai

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using System.Net.Mail;
using System.Net;

namespace BusinessLogicLayer
{
    public class MakeRequistionController
    {
        CategoryDAL ca = new CategoryDAL();
        MakeRequisitionDAL mr = new MakeRequisitionDAL();
        EmailNotificationControl emailController = new EmailNotificationControl();

        //to get all the category list from database
        public List<Category> getCatList()
        {
            var lst = ca.getCategoryList();
            return lst;
        }

        //to get all the item list by selected category
        public List<Stationery> getStationeryListByCatID(int catID)
        {
            var lst = ca.getItemListByCatID(catID);
            return lst;
        }

        
        public int getEmpIDFromUserName(String userName)
        {
            Employee emp = mr.getEmpInfo(userName);
            return emp.EmployeeID;
        }

        public int makeRequisition(int reqBy)
        {
            int i = mr.saveRequisition(reqBy);
            return i;
        }
        public void makeRequistionDetail(int reqID, String itemCode, int reqQty)
        {
            mr.saveReqDetail(reqID, itemCode, reqQty);
        }

        //send email to Acting Manager for Approval
        public void SendNotificationToDeptHead(String userName,int reqID)
        {
            Employee emp = mr.getEmpInfo(userName);
            Department dept = mr.getDeptInfoByEmpID(emp.EmployeeID);
            String headEmail = dept.Employee.Email;
            String empEmail = emp.Email;

            //if there is Acting Manager, send mail to acting manager
            try
            {
                if (dept.Employee2.EmployeeName != null)
                {
                    headEmail = dept.Employee2.Email;
                }
            }
            catch 
            {
                System.Diagnostics.Debug.Write("No Acting Manager");
            }
            

            List<RequisitionDetailOne> reqDetOneList = mr.getReqDetOneByReqID(reqID);
            
            try
            {
                MailMessage mailMessage = new MailMessage(empEmail, headEmail);

                mailMessage.Subject = "Stationery Request from " + emp.EmployeeName;
                mailMessage.Body = "Dear " + dept.Employee.EmployeeName + ",<br />Please help me to approve my stationery requisition! <br />Thanks.<br />";
                mailMessage.IsBodyHtml = true;
                mailMessage.Body += "<table border=1><tr><th>Description</th><th>Quantity</th></tr>";

                for (int i = 0; i < reqDetOneList.Count; i++)
                {
                    
                    Stationery stationery = mr.getStationeryInfoByItemCode(reqDetOneList[i].ItemCode);
                    String itemName = stationery.ItemName;
                    int reqQty = Convert.ToInt32(reqDetOneList[i].RequestQty);
                    mailMessage.Body += "<tr><td>"+ itemName + "</td><td>" + reqQty +"</td></tr>";

                }

                SmtpClient smtpClient = new SmtpClient("lynx.iss.nus.edu.sg");
                smtpClient.Send(mailMessage);
                smtpClient.Dispose();


            }
            catch (Exception ex)
            {

            }





        }
    }
}
